﻿#include "FITKAbaHistoryOutputManager.h"


void Interface::FITKAbaHistoryOutputManager::deleteStepHistoryOutput(int stepId)
{
    QList<FITKAbaHistoryOutput*> bdsToDelete{};
    int count = this->getDataCount();
    for (int i = 0; i< count; ++i)
    {
        FITKAbaHistoryOutput* history = this->getDataByIndex(i);
        if (history == nullptr)
            continue;
        if (history->getCreatedStep() == stepId)
            bdsToDelete.append(history);
    }
    for (FITKAbaHistoryOutput* b : bdsToDelete)
    {
        this->removeDataObj(b);
    }
}

void Interface::FITKAbaHistoryOutputManager::createDefaultHistoryOutput(FITKAbaAbstractStep* step)
{
    //当前的场输出个数
    int count = this->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        FITKAbaHistoryOutput* out = this->getDataByIndex(i);
        if (out->isValidInStep(step->getDataObjectID()))
            return;
    }
    FITKAbaHistoryOutput* output = new FITKAbaHistoryOutput();
    output->setParent(this);
    QString name = this->checkName("H-Output-1");
    output->setDataObjectName(name);
    output->setCreatedStep(step->getDataObjectID());
    this->appendDataObj(output);
    if (step->getStepType() == FITKAbaAbstractStep::FITKAbaStepType::DynamicExplicit)
    {
        output->setOutputFrequency(FITKAbaOutputVariable::OutputFrequency::EvenlySpacedTimeIntervals);
    }
    if (step->isPerturbation())
        output->setOutputFrequency(FITKAbaOutputVariable::OutputFrequency::AllModes);
}

void Interface::FITKAbaHistoryOutputManager::replaceStep(int oldStep, int newStep)
{
    int count = this->getDataCount();
    for (int i = 0; i < count; ++i)
    {
        FITKAbaHistoryOutput* data = this->getDataByIndex(i);
        if (data == nullptr)
            continue;
        if (data->getCreatedStep() != oldStep)
            continue;
        data->setCreatedStep(newStep);
        if (!data->isValidInCreatedStep())
            data->setActiveState(false);
    }
}
